// FrontEnd Plus GUI for JAD
// DeCompiled : HibernateTable.class

package middlegen.plugins.hibernate;

import java.util.*;
import middlegen.*;
import middlegen.javax.JavaTable;
import middlegen.plugins.hibernate.predicates.relation.NotTargetInOriginPK;
import middlegen.plugins.hibernate.predicates.relation.TargetInOriginPK;
import middlegen.plugins.hibernate.predicates.relation.TargetOneToOneFKRoles;
import middlegen.predicates.column.PrimaryKey;
import middlegen.util.BooleanNode;

// Referenced classes of package middlegen.plugins.hibernate:
//            HibernateColumn, HibernatePlugin

public class HibernateTable extends JavaTable
{

    private BooleanNode _lifecycle;
    private BooleanNode _validatable;
    private BooleanNode _equalsHashcode;
    private BooleanNode _proxy;
    private BooleanNode _plainCKey;
    private BooleanNode _mutable;
    private BooleanNode _dynamicUpdate;
    private BooleanNode _dynamicInsert;
    private BooleanNode _selectBeforeUpdate;
    private String _destinationClassName;
    private String _databaseSchema;
    private String _persister;
    private String _batchsize;
    private String _where;
    private String _classDescription;
    private String _classScope;
    private String _extends;
    private ArrayList _implements;
    private BooleanNode _pkInnerClass;
    private String _keyGenerator;
    private String _keyGeneratorArg;

    public HibernateTable(Table table)
    {
        super(table);
    }

    public void setImplementLifecycle(boolean flag)
    {
        setPrefsValue("lifecycle", Util.string(flag));
        _lifecycle.setValue(flag);
    }

    public void setEqualsHashcode(boolean flag)
    {
        setPrefsValue("equalshashcode", Util.string(flag));
        _equalsHashcode.setValue(flag);
    }

    public void setImplementValidatable(boolean flag)
    {
        setPrefsValue("validatable", Util.string(flag));
        _validatable.setValue(flag);
    }

    public void setPlainCompoundKey(boolean flag)
    {
        setPrefsValue("plainCKey", Util.string(flag));
        _plainCKey.setValue(flag);
    }

    public void setProxy(boolean flag)
    {
        setPrefsValue("proxy", Util.string(flag));
        _proxy.setValue(flag);
    }

    public void setMutable(boolean flag)
    {
        setPrefsValue("mutable", Util.string(flag));
        _mutable.setValue(flag);
    }

    public void setDynamicUpdate(boolean flag)
    {
        setPrefsValue("dynamicUpdate", Util.string(flag));
        _dynamicUpdate.setValue(flag);
    }

    public void setDynamicInsert(boolean flag)
    {
        setPrefsValue("dynamicInsert", Util.string(flag));
        _dynamicInsert.setValue(flag);
    }

    public void setSelectBeforeUpdate(boolean flag)
    {
        setPrefsValue("selectBeforeUpdate", Util.string(flag));
        _selectBeforeUpdate.setValue(flag);
    }

    public void setDestinationClassName(String value)
    {
        setPrefsValue("destinationClassName", value);
        _destinationClassName = value;
    }

    public void setDatabaseSchema(String value)
    {
        setPrefsValue("databaseSchema", value);
        _databaseSchema = value;
    }

    public void setPersister(String value)
    {
        setPrefsValue("persister", value);
        _persister = value;
    }

    public void setBatchSize(String value)
    {
        setPrefsValue("batchsize", value);
        _batchsize = value;
    }

    public void setWhere(String value)
    {
        setPrefsValue("where", value);
        _where = value;
    }

    public void setClassDescription(String value)
    {
        setPrefsValue("description", value);
        _classDescription = value;
    }

    public void setClassScope(String value)
    {
        setPrefsValue("classscope", value);
        _classScope = value;
    }

    public void setExtends(String value)
    {
        setPrefsValue("extends", value);
        _extends = value;
    }

    public void setImplements(ArrayList value)
    {
        setPrefsValue("implements", encodeImplementsStr(value));
        _implements = value;
    }

    public void setKeyGenerator(String value)
    {
        setPrefsValue("keygenerator", value);
        _keyGenerator = value;
    }

    public void setKeyGeneratorArg(String value)
    {
        setPrefsValue("keygeneratorarg", value);
        _keyGeneratorArg = value;
    }

    public String getDestinationClassName()
    {
        return _destinationClassName;
    }

    public String getCompoundKeyDestinationClassName()
    {
        return String.valueOf(String.valueOf(_destinationClassName)).concat("PK");
    }

    public String getDatabaseSchema()
    {
        return _databaseSchema.trim();
    }

    public String getPersister()
    {
        return _persister.trim();
    }

    public String getBatchSize()
    {
        return _batchsize.trim();
    }

    public String getWhere()
    {
        return _where.trim();
    }

    public String getClassDescription()
    {
        return _classDescription.trim();
    }

    public String getClassScope()
    {
        return _classScope.trim();
    }

    public String getExtends()
    {
        return _extends.trim();
    }

    public HibernateColumn getVersioningField()
    {
        Collection columns = getColumns();
        for(Iterator citr = columns.iterator(); citr.hasNext();)
        {
            HibernateColumn curCol = (HibernateColumn)citr.next();
            if(curCol.getColumnSpecialty().equals("version") || curCol.getColumnSpecialty().equals("timestamp"))
                return curCol;
        }

        return null;
    }

    public boolean isImplementLifecycle()
    {
        return _lifecycle.isCompletelyTrue();
    }

    public boolean isEqualsHashcode()
    {
        return _equalsHashcode.isCompletelyTrue();
    }

    public boolean isImplementValidatable()
    {
        return _validatable.isCompletelyTrue();
    }

    public boolean isProxy()
    {
        return _proxy.isCompletelyTrue();
    }

    public boolean isPlainCompoundKey()
    {
        return _plainCKey.isCompletelyTrue();
    }

    public boolean isMutable()
    {
        return _mutable.isCompletelyTrue();
    }

    public boolean isDynamicUpdate()
    {
        return _dynamicUpdate.isCompletelyTrue();
    }

    public boolean isDynamicInsert()
    {
        return _dynamicInsert.isCompletelyTrue();
    }

    public boolean isSelectBeforeUpdate()
    {
        return _selectBeforeUpdate.isCompletelyTrue();
    }

    public boolean isSchemaName()
    {
        return _databaseSchema != null && _databaseSchema.trim().length() != 0;
    }

    public boolean isPersisterName()
    {
        return _persister != null && _persister.trim().length() != 0;
    }

    public boolean isBatchSizeVal()
    {
        return _batchsize != null && _batchsize.trim().length() != 0;
    }

    public boolean isWhereVal()
    {
        return _where != null && _where.trim().length() != 0;
    }

    public boolean isClassDescriptionName()
    {
        return _classDescription != null && _classDescription.trim().length() != 0;
    }

    public boolean isClassScopeName()
    {
        return _classScope != null && _classScope.trim().length() != 0 && !_classScope.equals("public");
    }

    public boolean isExtendsName()
    {
        return _extends != null && _extends.trim().length() != 0;
    }

    public boolean isCompositeKey()
    {
        return getPkColumn() == null;
    }

    public boolean isVersioningPresent()
    {
        return getVersioningField() != null;
    }

    public BooleanNode getLifecycleNode()
    {
        return _lifecycle;
    }

    public BooleanNode getEqualsHashcodeNode()
    {
        return _equalsHashcode;
    }

    public BooleanNode getValidatableNode()
    {
        return _validatable;
    }

    public BooleanNode getPlainCompoundKeyNode()
    {
        return _plainCKey;
    }

    public BooleanNode getProxyNode()
    {
        return _proxy;
    }

    public BooleanNode getDynamicUpdateNode()
    {
        return _dynamicUpdate;
    }

    public BooleanNode getDynamicInsertNode()
    {
        return _dynamicInsert;
    }

    public BooleanNode getSelectBeforeUpdateNode()
    {
        return _selectBeforeUpdate;
    }

    public BooleanNode getMutableNode()
    {
        return _mutable;
    }

    public ArrayList getImplements()
    {
        return _implements;
    }

    public String getKeyGenerator()
    {
        return _keyGenerator.trim();
    }

    public String getKeyGeneratorArg()
    {
        return _keyGeneratorArg.trim();
    }

    public String getGeneratorUnsavedValue()
    {
        if("identity".equals(_keyGenerator))
            return "0";
        else
            return null;
    }

    public boolean isGeneratorRequiresParams()
    {
        return "hilo".equals(_keyGenerator) || "seqhilo".equals(_keyGenerator) || "sequence".equals(_keyGenerator);
    }

    public Collection getPrimaryKeyRoles()
    {
        return getRelationshipRoles(TargetInOriginPK.getInstance());
    }

    public Collection getNotPrimaryKeyRoles()
    {
        return getRelationshipRoles(NotTargetInOriginPK.getInstance());
    }

    public Collection getChildSidePrimaryKeyOnlyOneToOneRoles()
    {
        return getRelationshipRoles(TargetOneToOneFKRoles.getInstance());
    }

    public Collection getFkColsInactive(RelationshipRole role)
    {
        return getColsInactive(role, false);
    }

    public Collection getFkColsPkInactive(RelationshipRole role)
    {
        return getColsInactive(role, true);
    }

    public Collection getColsInactive(RelationshipRole role, boolean pkSelector)
    {
        Collection retVal = new ArrayList();
        if(!role.getRelation().isMany2Many())
        {
            ColumnMap cMap[] = role.getTargetRole().getColumnMaps();
            for(int i = 0; i < cMap.length; i++)
            {
                HibernateColumn col = (HibernateColumn)getColumn(cMap[i].getForeignKey());
                if(pkSelector)
                {
                    if(col.isPk())
                        retVal.add(col);
                    continue;
                }
                if(!col.isPk())
                    retVal.add(col);
            }

        }
        return retVal;
    }

    public boolean isRelationshipPkAndNonPkMembers()
    {
        boolean retVal = false;
        Collection rel = getNotPrimaryKeyRoles();
        Iterator itr = rel.iterator();
        do
        {
            if(!itr.hasNext())
                break;
            RelationshipRole role = (RelationshipRole)itr.next();
            if(!role.getRelation().isMany2Many())
            {
                RelationshipRole trole = role.getTargetRole();
                ColumnMap colMaps[] = trole.getColumnMaps();
                boolean pk = false;
                boolean nonPk = false;
                for(int i = 0; i < colMaps.length; i++)
                {
                    Column col = getColumn(colMaps[i].getForeignKey());
                    if(col.isPk())
                        pk = true;
                    else
                        nonPk = true;
                }

                if(pk && nonPk)
                    retVal = true;
            }
        } while(true);
        return retVal;
    }

    public boolean isRoleOneToOnePrimaryKeys(RelationshipRole role, HibernateTable target)
    {
        boolean retVal = true;
        ColumnMap cMap[] = role.getColumnMaps();
        if(cMap.length <= 0)
            return false;
        int roleColCount = cMap.length;
        for(int i = 0; i < cMap.length; i++)
        {
            HibernateColumn col_lefts = (HibernateColumn)getColumn(cMap[i].getPrimaryKey());
            HibernateColumn col_rights = (HibernateColumn)target.getColumn(cMap[i].getForeignKey());
            if(!col_rights.isPk() || !col_lefts.isPk())
                return false;
        }

        int lPKCount = getColumns(PrimaryKey.getInstance()).size();
        if(lPKCount != roleColCount)
            return false;
        int rPKCount = target.getColumns(PrimaryKey.getInstance()).size();
        if(rPKCount != roleColCount)
            return false;
        else
            return retVal;
    }

    public boolean isFKRoleHasAnyEqualsHashcode(RelationshipRole role)
    {
        boolean retVal = false;
        ColumnMap cMap[] = role.getTargetRole().getColumnMaps();
        if(cMap.length <= 0)
            return false;
        int roleColCount = cMap.length;
        for(int i = 0; i < cMap.length; i++)
        {
            HibernateColumn col = (HibernateColumn)getColumn(cMap[i].getForeignKey());
            if(col.isIncludeEquals())
                return true;
        }

        return retVal;
    }

    public boolean isCompoundKeyHasAnyEqualsHashcode()
    {
        boolean retVal;
label0:
        {
            retVal = false;
            if(!isCompositeKey())
                break label0;
            Iterator itr = getPrimaryKeyColumns().iterator();
            HibernateColumn col;
            do
            {
                if(!itr.hasNext())
                    break label0;
                col = (HibernateColumn)itr.next();
            } while(!col.isIncludeEquals());
            return true;
        }
        return retVal;
    }

    public String getCardinalityValue(RelationshipRole role)
    {
        if(role.getRelation().isMany2Many())
            return "many-to-many";
        if(role.getRelation().isOne2One())
            return "one-to-one";
        if(role.isTargetMany())
            return "one-to-many";
        else
            return "many-to-one";
    }

    public String getDirectionality(RelationshipRole role)
    {
        if(role.getRelation().isBidirectional())
            return "bi-directional";
        else
            return "uni-directional";
    }

    protected void init()
    {
        super.init();
        HibernatePlugin plugin = (HibernatePlugin)getPlugin();
        _lifecycle = plugin.getLifecycleNode().createChild(plugin.isLifecycle());
        if(getPrefsValue("lifecycle") != null)
            setImplementLifecycle(Util.bool(getPrefsValue("lifecycle")));
        _validatable = plugin.getValidatableNode().createChild(plugin.isValidatable());
        if(getPrefsValue("validatable") != null)
            setImplementValidatable(Util.bool(getPrefsValue("validatable")));
        _equalsHashcode = plugin.getEqualsHashcodeNode().createChild(plugin.isEqualsHashcode());
        if(getPrefsValue("equalshashcode") != null)
            setEqualsHashcode(Util.bool(getPrefsValue("equalshashcode")));
        _plainCKey = plugin.getPlainCompoundKeyNode().createChild(plugin.isPlainCompoundKeys());
        if(getPrefsValue("plainCKey") != null)
            setPlainCompoundKey(Util.bool(getPrefsValue("plainCKey")));
        _proxy = plugin.getProxyNode().createChild(plugin.isProxy());
        if(getPrefsValue("proxy") != null)
            setProxy(Util.bool(getPrefsValue("proxy")));
        _mutable = plugin.getMutableNode().createChild(plugin.isMutable());
        if(getPrefsValue("mutable") != null)
            setMutable(Util.bool(getPrefsValue("mutable")));
        _dynamicUpdate = plugin.getDynamicUpdateNode().createChild(plugin.isDynamicUpdate());
        if(getPrefsValue("dynamicUpdate") != null)
            setDynamicUpdate(Util.bool(getPrefsValue("dynamicUpdate")));
        _dynamicInsert = plugin.getDynamicInsertNode().createChild(plugin.isDynamicInsert());
        if(getPrefsValue("dynamicInsert") != null)
            setDynamicInsert(Util.bool(getPrefsValue("dynamicInsert")));
        _selectBeforeUpdate = plugin.getSelectBeforeUpdateNode().createChild(plugin.isSelectBeforeUpdate());
        if(getPrefsValue("selectBeforeUpdate") != null)
            setSelectBeforeUpdate(Util.bool(getPrefsValue("selectBeforeUpdate")));
        _databaseSchema = plugin.getDatabaseSchema();
        if(getPrefsValue("databaseSchema") != null)
            setDatabaseSchema(getPrefsValue("databaseSchema"));
        _classDescription = plugin.getClassDescription();
        if(getPrefsValue("description") != null)
            setClassDescription(getPrefsValue("description"));
        _classScope = plugin.getClassScope();
        if(getPrefsValue("classscope") != null)
            setClassScope(getPrefsValue("classscope"));
        _extends = plugin.getExtends();
        if(getPrefsValue("extends") != null)
            setExtends(getPrefsValue("extends"));
        _implements = plugin.getImplements();
        if(getPrefsValue("implements") != null)
            setImplements(decodeImplementsStr(getPrefsValue("implements"), _implements));
        _persister = plugin.getPersister();
        if(getPrefsValue("persister") != null)
            setPersister(getPrefsValue("persister"));
        _batchsize = plugin.getBatchSize();
        if(getPrefsValue("batchsize") != null)
            setBatchSize(getPrefsValue("batchsize"));
        _where = "";
        if(getPrefsValue("where") != null)
            setWhere(getPrefsValue("where"));
        if(getPrefsValue("destinationClassName") != null)
            _destinationClassName = getPrefsValue("destinationClassName");
        else
            _destinationClassName = String.valueOf(getBaseClassName()) + String.valueOf(plugin.getBeansuffix());
        _keyGenerator = plugin.getStandardGeneratorScheme();
        if(getPrefsValue("keygenerator") != null)
            _keyGenerator = getPrefsValue("keygenerator");
        _keyGeneratorArg = "";
        if(getPrefsValue("keygeneratorarg") != null)
            _keyGeneratorArg = getPrefsValue("keygeneratorarg");
    }

    private String encodeImplementsStr(ArrayList orig)
    {
        StringBuffer encodeBuffer = new StringBuffer();
        for(int i = 0; i < orig.size(); i++)
        {
            encodeBuffer.append((String)orig.get(i));
            if(i + 1 < orig.size())
                encodeBuffer.append(",");
        }

        return encodeBuffer.toString();
    }

    private ArrayList decodeImplementsStr(String encodedStr, ArrayList orig)
    {
        ArrayList retVal = orig;
        if(encodedStr != null && encodedStr.length() != 0)
        {
            retVal = new ArrayList();
            StringTokenizer st = new StringTokenizer(encodedStr, ",");
            do
            {
                if(!st.hasMoreTokens())
                    break;
                String token = st.nextToken();
                if(token.length() > 0)
                    retVal.add(token);
            } while(true);
        }
        return retVal;
    }

    public String getVariableName()
    {
        return Util.decapitalise(getBaseClassName());
    }
}
